using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using umbraco.cms.presentation.Trees;
using umbraco.interfaces;
using umbraco.controls;
using umbraco.cms.businesslogic.language;
using Commerce.Common;
using umbraco.BusinessLogic.Actions;

namespace CommerceForUmbraco.adminSection.trees
{
    public class loadOrderTree : BaseTree
    {
        public loadOrderTree(string application) : base(application) { }

        protected override void CreateRootNode(ref XmlTreeNode rootNode)
        {
            rootNode.Icon = FolderIcon;
            rootNode.OpenIcon = FolderIconOpen;
            rootNode.NodeType = "init" + TreeAlias;
            rootNode.NodeID = "init";
        }

        public override void Render(ref XmlTree tree)
        {

            string year = string.Empty;
            string month = string.Empty;
            string day = string.Empty;
            if (HttpContext.Current.Request.QueryString.ToString().IndexOf("year") >= 0)
            {
                year = HttpContext.Current.Request.QueryString.Get("year");
            }
            if (HttpContext.Current.Request.QueryString.ToString().IndexOf("month") >= 0)
            {
                month = HttpContext.Current.Request.QueryString.Get("month");
            }
            if (HttpContext.Current.Request.QueryString.ToString().IndexOf("day") >= 0)
            {
                day = HttpContext.Current.Request.QueryString.Get("day");
            }


            if (year != string.Empty)
            {
                int currentyear = Convert.ToInt32(year);

                if (month != string.Empty)
                {
                    int currentmonth = Convert.ToInt32(month);


                    if (day != string.Empty)
                    {

                        int currentday = Convert.ToInt32(day);

                        // get collection values
                        Commerce.Common.OrderCollection orders = null;

                        int lastDayOfThisMonth = new DateTime(currentyear, currentmonth, 1).AddMonths(1).AddDays(-1).Day;

                        orders = new OrderCollection().Where("orderStatusID", SubSonic.Comparison.NotEquals, OrderStatus.NotProcessed).Where(Order.Columns.OrderDate, SubSonic.Comparison.GreaterOrEquals, new DateTime(currentyear, currentmonth, currentday,0,0,0)).Where(Order.Columns.OrderDate, SubSonic.Comparison.LessOrEquals, new DateTime(currentyear, currentmonth, currentday,23,59,59)).Load();

                        foreach (Order thisOrder in orders)
                        {
                            XmlTreeNode xNode = XmlTreeNode.Create(this);
                            xNode.NodeID = thisOrder.OrderID.ToString();
                            xNode.Text = thisOrder.LastName + ", " + thisOrder.FirstName + " (" + thisOrder.OrderNumber + ") " + thisOrder.OrderStatus.ToString();
                            xNode.Action = "javascript:openOrder(" + thisOrder.OrderID + ");";
                            xNode.Icon = "statistik.gif";
                            xNode.OpenIcon = "statistik.gif";
                            xNode.Menu = new System.Collections.Generic.List<IAction>();

                            tree.Add(xNode);
                        }

                    }
                    else
                    {
                        int lastDayOfThisMonth = new DateTime(currentyear, currentmonth, 1).AddMonths(1).AddDays(-1).Day;

                        SubSonic.Query test = new SubSonic.Query(Tables.Order);
                        test.SelectList = "Day(orderDate)";
                        test.AddWhere("orderStatusID", SubSonic.Comparison.NotEquals, OrderStatus.NotProcessed);
                        test.AddWhere(Order.Columns.OrderDate, SubSonic.Comparison.GreaterOrEquals, new DateTime(currentyear, currentmonth, 1));
                        test.AddWhere(Order.Columns.OrderDate, SubSonic.Comparison.LessOrEquals, new DateTime(currentyear, currentmonth, lastDayOfThisMonth));
                        test.DISTINCT();
                        test.ORDER_BY("1");
                        DataSet days = test.ExecuteDataSet();

                        foreach (DataRow daysrow in days.Tables[0].Rows)
                        {
                            XmlTreeNode xNode = XmlTreeNode.Create(this);
                            xNode.NodeID = daysrow[0].ToString();
                            xNode.Text = daysrow[0].ToString();
                            xNode.Icon = FolderIcon;
                            xNode.OpenIcon = FolderIconOpen;
                            xNode.Source = "tree.aspx?day=" + daysrow[0].ToString() +"&month=" + currentmonth.ToString() + "&year=" + currentyear.ToString() + "&app=" + m_app + "&treeType=" +
                                    HttpContext.Current.Request.QueryString["treeType"] + "&rnd=" + Guid.NewGuid();
                            tree.Add(xNode);
                        }
                    }
                }
                else
                {
                    SubSonic.Query test = new SubSonic.Query(Tables.Order);
                    test.SelectList = "MONTH(orderDate)";
                    test.AddWhere("orderStatusID", SubSonic.Comparison.NotEquals, OrderStatus.NotProcessed);
                    test.AddWhere(Order.Columns.OrderDate, SubSonic.Comparison.GreaterOrEquals, new DateTime(currentyear - 1, 12, 31));
                    test.AddWhere(Order.Columns.OrderDate, SubSonic.Comparison.LessOrEquals, new DateTime(currentyear, 12, 31));
                    test.DISTINCT();
                    test.ORDER_BY("1");
                    
                    DataSet months = test.ExecuteDataSet();

                    foreach (DataRow monthsrow in months.Tables[0].Rows)
                    {
                        XmlTreeNode xNode = XmlTreeNode.Create(this);
                        xNode.NodeID = monthsrow[0].ToString();
                        xNode.Text = new DateTime(2000,Convert.ToInt32(monthsrow[0].ToString()),1).ToString("MMMM");
                        xNode.Icon = FolderIcon;
                        xNode.OpenIcon = FolderIconOpen;
                        xNode.Source = "tree.aspx?month=" + monthsrow[0].ToString() + "&year=" + currentyear.ToString() + "&app=" + m_app + "&treeType=" +
                                HttpContext.Current.Request.QueryString["treeType"] + "&rnd=" + Guid.NewGuid();
                        tree.Add(xNode);
                    }
                }
            }
            else
            {
                SubSonic.Query test = new SubSonic.Query(Tables.Order);
                test.SelectList = "YEAR(orderDate)";
                test.AddWhere("orderStatusID", SubSonic.Comparison.NotEquals, OrderStatus.NotProcessed);
                test.AddWhere("orderStatusID", SubSonic.Comparison.GreaterThan, 0);
                test.DISTINCT();

                test.ORDER_BY("1");
                DataSet years = test.ExecuteDataSet();

                foreach (DataRow yearrow in years.Tables[0].Rows)
                {
                    XmlTreeNode xNode = XmlTreeNode.Create(this);
                    xNode.NodeID = yearrow[0].ToString();
                    xNode.Text = yearrow[0].ToString();
                    xNode.Icon = FolderIcon;
                    xNode.OpenIcon = FolderIconOpen;
                    xNode.Source = "tree.aspx?year=" + yearrow[0].ToString() + "&app=" + m_app + "&treeType=" +
                            HttpContext.Current.Request.QueryString["treeType"] + "&rnd=" + Guid.NewGuid();
                    tree.Add(xNode);
                }
            }
        }

        public override void RenderJS(ref StringBuilder Javascript)
        {
            Javascript.Append(
                @"
                    function openOrder(id) {
	                    parent.right.document.location.href = 'commerce/editOrder.aspx?o=' + id;
                    }
                ");
        }

        protected override void CreateRootNodeActions(ref System.Collections.Generic.List<IAction> actions)
        {
            actions.Clear();
            actions.Add(ActionRefresh.Instance);
        }
    }
}
